document.addEventListener('DOMContentLoaded', function () {
    var msgTextArea = document.getElementById('input_message')
    var publishBtn = document.getElementById('publishBtn')
    msgTextArea.addEventListener('input', function () {
        var msg = msgTextArea.value
        if (msg.toString().trim().length > 0) {
            publishBtn.disabled = false
        } else {
            publishBtn.disabled = true
        }
    })
    publishBtn.addEventListener('click', function () {
        var msg = msgTextArea.value
        var request = new Request('./publish.do', {
            method: 'POST',
            headers: {
                'Content-Type': 'text/plain'
            },
            body: msg
        })
        fetch(request).then(function (resp) {
            return resp.json()
        })
            .then(function (res) {
                msgTextArea.value = ''
                var myarticles = document.getElementById('myarticles')
                myarticles.insertAdjacentHTML(
                    'afterbegin',
                    '<div class = "card">'
                    + '<div class = "card-body">'
                    + res.content
                    + '<div class = "text-right">'
                    + res.publish
                    + '</div>'
                    + '</div>'
                    + '</div>'
                )
            })
            .catch(function (err) {
                console.log(err)
            })
    })
})